﻿using System;
using System.Windows.Forms;
using System.Net;
using monitor_2_DBwork;

namespace monitor_server_2
{
    public delegate string[] GetAllRegDelegate();
    public delegate string GetSelRegDelegate();
    public delegate void TreeUpdateDelegate();

    public partial class TrustedAddressList : Form
    {
        private bool _success, _loginSuccess;

        public TrustedAddressList()
        {
            //StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void TreeUpdate()
        {
            regionTree.Nodes.Clear();
            TreeNode[] nodes;
            try
            {
                nodes = DBwork.GetTrustedAddressTree();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Main.Rm.GetString("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                _success = false;
                return;
            }
            if (nodes[0].Name.Contains("err:"))
            {
                MessageBox.Show(nodes[0].Name.Substring(4), Main.Rm.GetString("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                _success = false;
                return;
            }
            regionTree.Nodes.AddRange(nodes);
            regionTree.ExpandAll();
            _success = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _loginSuccess = new LoginForm(LoginForm.AccessType.Admin).ShowDialog() == DialogResult.Yes;
            if (!_loginSuccess)
                return;
            Text = Main.Rm.GetString("TrustedAddressesList");
            менюToolStripMenuItem.Text = Main.Rm.GetString("Menu");
            закрытьToolStripMenuItem.Text = Main.Rm.GetString("Close");
            добавитьToolStripMenuItem.Text = Main.Rm.GetString("AddNewTrustedAddress");
            удалитьToolStripMenuItem.Text = Main.Rm.GetString("Delete");
            TreeUpdate();
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeUpdateDelegate updateTree = TreeUpdate;
            GetAllRegDelegate regions = GetAllRegions;
            var addMenu = new AddTrustedAddress(regions, _selRegion, updateTree);
            addMenu.ShowDialog();            
        }

        string[] GetAllRegions()
        {
            var regions = new string[6];
            int i = 0;
            foreach (TreeNode node in regionTree.Nodes)
            {
                regions[i++] = node.Text;
            }
            return regions;
        }

        string GetSelectedRegion()
        {
            if (regionTree.SelectedNode.Text.Contains("область"))
                return regionTree.SelectedNode.Text;
            return regionTree.SelectedNode.Parent.Text;
        }

        private void закрытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        GetSelRegDelegate _selRegion;
        private void regionTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            _selRegion = GetSelectedRegion;
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPAddress ip;
            if (!IPAddress.TryParse(regionTree.SelectedNode.Text, out ip))
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + @": " + Main.Rm.GetString("IPAddress"));
                return;
            }
            var result = MessageBox.Show(Main.Rm.GetString("DeleteAsk")
                          , Main.Rm.GetString("ConfirmDeleting")
                          , MessageBoxButtons.YesNo
                          , MessageBoxIcon.Question);
            if (result == DialogResult.No)
                return;
            if (DBwork.DeleteData("trustedAddress", ip.ToString()))
            {
                MessageBox.Show(Main.Rm.GetString("Complete"));
                TreeUpdate();
            }
            else
            {
                MessageBox.Show(Main.Rm.GetString("Error"));
            }
            
        }

        private void TrustedAddressList_Shown(object sender, EventArgs e)
        {
            if (!_success || !_loginSuccess)
                Close();
            Icon = (System.Drawing.Icon) Main.Rm.GetObject("trustedList_16_ico");
        }
    }
}
